//
//  SurveyService.m
//  HZHome
//
//  Created by zmingchun on 14-0-27.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//  民情调查服务类
//

#import "SurveyService.h"
#import "UIImageView+WebCache.h"
#import "SurveyMasterEntity.h"
#import "DataStoreService.h"
#import "SurveyItemCell.h"
#import "UserInfoService.h"

@implementation SurveyService
//请求参数集
NSMutableDictionary *params;

- (void)loadDataWithKey:(NSString *)keyOrNil{
    _currentPageNo = 1;
    if (self.dataList) {
        self.dataList = nil;
    }
    //民情调查
    _dataItemName = @"SurveyMasterEntity";
    NSDictionary *_config = [super paramDic];
    
    params = [NSMutableDictionary dictionary];
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    [params setValue:userInfo.userInfo.Id forKey:@"userID"];
    [params setValue:[_config objectForKey:@"communityId"] forKey:@"communityId"];
    [params setValue:[_config objectForKey:@"partyMember"] forKey:@"partyMember"];
    if (keyOrNil) {
        [params setValue:keyOrNil forKey:@"surveyTitle"];
    } else {
        [params setValue:@"" forKey:@"surveyTitle"];
    }
    [self doHttpPost:SERVER_URL_SURVEY_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo] parameters:params];
}

- (void)loadMoreDataWithKey:(NSString *)keyOrNil{
    if (!params || [params count]==0) {
         params = [NSMutableDictionary dictionary];
    }
    if (keyOrNil) {
        [params setValue:keyOrNil forKey:@"surveyTitle"];
    } else {
        [params setValue:@"" forKey:@"surveyTitle"];
    }
    //民情调查
    _dataItemName = @"SurveyMasterEntity";
    [self doHttpPost:SERVER_URL_SURVEY_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo + 1] parameters:params];
}

- (CGFloat)heightForCellAtRow:(NSIndexPath *)index{
        return 90.0f;//民情调查每行88
}

/**展示数据*/
- (void)configCell:(UITableViewCell *)cell atIndex:(NSIndexPath *)index{
    if (self.dataList == nil || self.dataList.count == 0) {
        return;
    }
        //民情调查
        SurveyMasterEntity *item = [self.dataList objectAtIndex:index.row];
        SurveyItemCell *tempCell = (SurveyItemCell *)cell;
        tempCell.titleLabel.text = [CommonUtil dealNullObject:item.surveyTitle];
        tempCell.detailInfoView.text = [CommonUtil dealNullObject:item.surveyContext];
        if (item.countNum && [item.countNum intValue]>0) {//已投票
            tempCell.hasVote.hidden = NO;
        }else{
            if(item.isEnd && [item.isEnd intValue]==0){//已结束
             tempCell.hasVote.text = @"已结束";
             tempCell.hasVote.textColor = [UIColor darkGrayColor];
             tempCell.hasVote.hidden = NO;
            }else{
             tempCell.hasVote.hidden = YES;
            }
        }
        //图片
        NSString *pic = [NSString stringWithFormat:@"http://%@%@", SURVEY_PIC_BASE_URL, item.surveyImage];
    
        __block UIActivityIndicatorView *activityIndicator;
        __weak UIImageView *weakImageView = tempCell.imageView;
        [tempCell.imageView setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageProgressiveDownload|SDWebImageRetryFailed|SDWebImageLowPriority progress:^(NSUInteger receivedSize, long long expectedSize)
         {
             if (!activityIndicator)
             {
                 [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                 activityIndicator.center = weakImageView.center;
                 [activityIndicator startAnimating];
             }
         } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
         {
             [activityIndicator removeFromSuperview];
             activityIndicator = nil;
         }];
}

@end
